var pattern = new RegExp(/\?Code=(\S*)/g);
var idx = pattern.exec(window.location.href);
var Picture_PC_Code, Picture_M_Code, Content_PC = '', Content_M = '';

var ItemCode = $("input[data-field='ItemCode']"),
    Name = $("input[data-field='ItemName']"),
    ListPicCode = $('#ListPicCode'),
    ListPicName = $('#ListPicName'),
    ListPiclocation = $('#ListPiclocation');

$(function(){
    requestAjax();
    $('.u_pictures .u_item').on('click', addPictures);
    $("li[data-opt='add']").on('click', addCode);
    $("li[data-opt='back']").on('click', back);
    setContent('iframePC', Content_PC);
    setContent('iframeM', Content_M);
    $("#js_submit, li[data-opt='save']").on("click", saveSubmit);
});

function requestAjax(){ //MDM000603
    if(idx){
        var _postData = Object.create(postData);    //请求头
        _postData.Method = 'Q',
        _postData.Content = {
            "Code": idx[1]
        }
        $.ajax({
            async: false,
            type: 'post', 
            url: mdmPath + '/MDM0006/MDM000603',
            data: _postData,
            dataType: 'json',
            success: function (re) {
                loadAjax(re);
            }
        });
    }
}
function loadAjax(re){
    if(re.Status==200){
        //console.log(re.Content)
        ItemCode.val(re.Content.Code); //代码
        Name.val(re.Content.Name); //名称
        Content_PC = re.Content.Content_PC; //PC详情
        Content_M = re.Content.Content_M;   //Mobile详情
        ListPicCode.val(re.Content.ListPicCode);    //列表图片
        ListPicName.val(re.Content.ListPicName);
        ListPiclocation.val(re.Content.ListPiclocation);
        if(re.Content.Picture_PC_Address){  //PC图片
            Picture_PC_Code = re.Content.Picture_PC_Code;
            $("li[data-tab='detailPC'] .u_item").html('<img src="'+PicAddress+re.Content.Picture_PC_Address+'">');
        }
        if(re.Content.Picture_M_Address){  //Mobile图片
            Picture_M_Code = re.Content.Picture_M_Code;
            $("li[data-tab='detailMob'] .u_item").html('<img src="'+PicAddress+re.Content.Picture_M_Address+'">');
        }
        $("#createDate").val(re.Content.CreateDate);    //
        $("#updateDate").val(re.Content.UpdateDate);    //
        $("#createUser").val(re.Content.CreateUser);    //
        $("#updateUser").val(re.Content.UpdateUser);    //
    } else if (re.Status == -999) {
        alert_QX($('body'), re.Message);
    }else{
        myAlert(re.Message);
    }
}

//Options
function addCode(){
    window.open('MDM000602.html', '_blank'); 
}
function back(){
    window.open('MDM000601.html', '_self');
}
function saveSubmit(){  //MDM000601
    var Picture_PC_Code = "", Picture_PC_Address = "";
    if($("li[data-tab='detailPC'] .u_item img").length>0){  //如果img存在，获取img地址
        var src_P = $("li[data-tab='detailPC'] .u_item img")[0].src;
        if(src_P.indexOf(PicAddress)!=-1){
            Picture_PC_Address = src_P.slice(PicAddress.length);
            var tempArr = Picture_PC_Address.split('/');
            var tempCode = tempArr[tempArr.length-1];
            Picture_PC_Code = tempCode.split('.')[0];
        }
    }
    var Picture_M_Code = "", Picture_M_Address = "";
    if($("li[data-tab='detailMob'] .u_item img").length>0){  //如果img存在，获取img地址
        var src_M = $("li[data-tab='detailMob'] .u_item img")[0].src;
        if(src_M.indexOf(PicAddress)!=-1){
            Picture_M_Address = src_P.slice(PicAddress.length);
            var tempArr = Picture_M_Address.split('/');
            var tempCode = tempArr[tempArr.length-1];
            Picture_M_Code = tempCode.split('.')[0];
        }
    }
    var requestData = {
        Content: {
            Code: ItemCode.val(),
            Name: Name.val(),
            Content_PC: getContent('iframePC'),
            Picture_PC_Code: Picture_PC_Code,
            Picture_PC_Address: Picture_PC_Address,
            Content_M: getContent('iframeM'),
            Picture_M_Code: Picture_M_Code,
            Picture_M_Address: Picture_M_Address,
            ListPicCode: ListPicCode.val(),
            ListPicName: ListPicName.val(),
            ListPiclocation: ListPiclocation.val(),
        },   
    }
    if(idx){
        requestData.Method = 'U'
    }else{
        requestData.Method = 'A'
    }
    for(i in postData){
        requestData[i] = postData[i]
    }
    console.log(requestData)
    $.ajax({
        type: "post",
        url: mdmPath + '/MDM0006/MDM000601',
        data: requestData,
        dataType: "json",
        success: function(re) {
            if(re.Status==200){
                myAlert('保存成功')
            }else{
                myAlert(re.Message)
            }
        }
    });
}
